package stuttgart.uni.locationtracker;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.util.Log;

public class ObjectArrayConverter {

	public static byte[] toByteArrayfromObject(Object obj){
		  byte[] bytes = null;
		  ByteArrayOutputStream bos = new ByteArrayOutputStream();
		  try {
		    ObjectOutputStream oos = new ObjectOutputStream(bos); 
		    oos.writeObject(obj);
		    oos.flush(); 
		    oos.close(); 
		    bos.close();
		    bytes = bos.toByteArray();
		  }
		  catch (IOException ex) {
			  Log.d("LocationTracker","IOException in toByteArrayfromObject");
		  }
		  return bytes;
		}
		    
		public static Object toObjectfromByteArray(byte[] bytes){
		  Object obj = null;
		  try {
		    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
		    ObjectInputStream ois = new ObjectInputStream(bis);
		    obj = ois.readObject();
		  }
		  catch (IOException ex) {
			 Log.d("LocationTracker","IOException in toObjectfromByteArray");
		  }
		  catch (ClassNotFoundException ex) {
			  Log.d("LocationTracker","ClassNotFoundException in toObjectfromByteArray");
		  }
		  return obj;
		}
}
